/*
* @Author: qingsong-Liu
* @Date:   2019-08-22 12:36:06
* @Last Modified by:   qingsong-Liu
* @Last Modified time: 2019-09-01 14:37:34
*/

require('./index.css');
require('page/common/header/index.js');
require('page/common/nav/index.js');
var _mm=require('util/mm.js');
var _order=require('service/order-service.js');
var _address=require('service/address-service.js');
var templateAddress=require('./address-list.string');
var templateProduct=require('./product-list.string');
var addressModal=require('./address-modal.js');

var page={
    data:{
        selectdAddressId : null
    },
    init:function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad:function(){
        this.loadAddressList();
        this.loadProductList();
    },
    bindEvent:function(){
        var _this=this;
        // 地址的选择
        $(document).on('click','.address-item',function(){
            $(this).addClass('active')
                .siblings('.address-item').removeClass('active');
            _this.data.selectdAddressId = $(this).data('id');
        });
        // 订单的提交
        $(document).on('click','.order-submit',function(){
            var shippingId = _this.data.selectdAddressId;
            if (shippingId) {
                _order.createOrder({
                    shippingId : shippingId
                },function(res){
                    window.location.href = './payment.html?orderNumber='+res.orderNo;
                },function(errMsg){
                    _mm.errorTips(errMsg);
                });
            }else{
                _mm.errorTips('请选择地址后再提交');
            }
        });
        // 地址的添加
        $(document).on('click','.address-add',function(){
            addressModal.show({
                isUpdate : false,
                onSuccess : function(){
                    _this.loadAddressList();
                }
            });
        });
        // 地址的编辑
        $(document).on('click','.address-update',function(e){
            // 禁止事件往上冒泡
            e.stopPropagation();
            var shippingId = $(this).parents('.address-item').data('id');
            _address.getAddress(shippingId,function(res){
                addressModal.show({
                    isUpdate    : true,
                    data        : res,
                    onSuccess   : function(){
                        _this.loadAddressList();
                    }
                });
            },function(errMsg){
                _mm.errorTips(errMsg);
            });
        });
        // 地址的删除
        $(document).on('click','.address-delete',function(e){
            // 禁止事件往上冒泡
            e.stopPropagation();
            var id = $(this).parents('.address-item').data('id');
            if (window.confirm('确认要删除改地址吗？')) {
                _address.deleteAddress(id,function(res){
                    _this.loadAddressList();
                },function(errMsg){
                    _mm.errorTips(errMsg);
                });
            }
        });

    },
    // 加载地址列表
    loadAddressList:function(){
        var _this=this;
        $('.address-con').html('<div class="loading"></div>');
        // 获取地址列表
        _address.getAddressList(function(res){
            _this.addressFilter(res);
            var addressListHtml = _mm.renderHtml(templateAddress,res);
            $('.address-con').html(addressListHtml);
        },function(errMsg){
            $('.address-con').html('<p class="err-tip">地址加载失败，请刷新后重试</p>');
        });
    },
    // 处理地址列表中选中状态
    addressFilter:function(data){
        if (this.data.selectdAddressId) {
            var selectdAddressIdFlag = false;
            for(var i=0, length = data.list.length; i < length; i++){
                if (data.list[i].id === this.data.selectdAddressId) {
                    data.list[i].isActive = true;
                    selectdAddressIdFlag = true;
                }
            }
            // 如果以前选中的地址不在列表里，将其删除
            if (!selectdAddressIdFlag) {
                this.data.selectdAddressId = null;
            }
        }
    },
    // 加载商品清单
    loadProductList:function(){
        $('.product-con').html('<div class="loading"></div>');
        // 获取商品列表
        _order.getProductList(function(res){
            var productListHtml = _mm.renderHtml(templateProduct,res);
            $('.product-con').html(productListHtml);
        },function(errMsg){
            $('.product-con').html('<p class="err-tip">商品信息加载失败，请刷新后重试</p>');
        });
    }
};
$(function(){
    page.init();
})